﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities.Excel.Xml;
using Common.Utilities.Excel.Import;
using System.Web;
using System.IO;
using System.Web.Caching;

namespace Common.Utilities.Excel.Export
{
    public class ExcelExportConfiguration
    {
        #region 字段

        /// <summary>
        /// 配置文件路径
        /// </summary>
        private const string ConfigFilePath = "App_Config/Excel/ExcelExport.config";

        private static FileSystemWatcher watcher;

        /// <summary>
        /// 将Excel导出的配置信息存储与Cahce中的Key名称
        /// </summary>
        public static string CacheKey = "Common.Utilities.Excel";
        private XmlDocument xmlDoc;
        private ExcelExportProviderCollection _excels;

        #endregion

        #region 属性

        public ExcelExportProviderCollection ExcelConfigs
        {
            get { return _excels; }
        }

        #endregion

        #region 构造方法

        public ExcelExportConfiguration() { }

        public ExcelExportConfiguration(XmlDocument xdoc)
        {
            xmlDoc = xdoc;
            _excels = new ExcelExportProviderCollection();
            Initialize();
        }

        private static ExcelExportConfiguration excelExportConfiguration;
        /// <summary>
        /// 获取 ExcelExportConfiguration 配置类的唯一实例。
        /// </summary>
        /// <value>ExcelExportConfiguration 配置类的唯一实例。</value>
        public static ExcelExportConfiguration Current
        {
            get
            {
                if (excelExportConfiguration == null)
                {
                    String path = null;
                    XmlDocument doc = new XmlDocument();
                    HttpContext context = HttpContext.Current;
                    if (context != null)
                    {
                        path = context.Server.MapPath("~/" + ConfigFilePath);
                    }
                    else
                    {
                        path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigFilePath);
                    }
                    doc.Load(path);
                    excelExportConfiguration = new ExcelExportConfiguration(doc);

                    if (watcher == null)
                    {
                        watcher = new FileSystemWatcher(Path.GetDirectoryName(path), "ExcelExport.config");
                        watcher.Changed += new FileSystemEventHandler(watcher_Changed);
                        watcher.EnableRaisingEvents = true;
                    }
                }
                return excelExportConfiguration;
            }
        }

        static void watcher_Changed(object sender, FileSystemEventArgs e)
        {
            excelExportConfiguration = null;
        }

        #endregion

        #region 方法

        private void Initialize()
        {
            XmlNode rootNode = GetConfigSection("/ExportConfigs");
            foreach (XmlNode childNode in rootNode.ChildNodes)
            {
                switch (childNode.Name.ToLower())
                {
                    case "excel":
                        if (!string.IsNullOrEmpty(XmlUtility.getNodeAttributeStringValue(childNode, "typename")))
                        {
                            ExcelExportProvider providerType = new ExcelExportProvider(childNode);
                            _excels.Add(providerType);
                        }
                        break;
                }
            }
        }

        /// <summary>
        /// 读取配置文件中指定的节点。
        /// </summary>
        /// <param name="nodePath">节点 XPath 路径。</param>
        /// <returns>返回指定的节点或 null 值。</returns>
        public XmlNode GetConfigSection(String nodePath)
        {
            return this.xmlDoc.SelectSingleNode(nodePath);
        }

        #endregion
    }
}
